<?php

namespace App\Exports;

use App\Models\Cert\CertGo;
use Maatwebsite\Excel\Concerns\FromCollection;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
class GoExport extends DefaultValueBinder implements FromArray,WithCustomValueBinder
{

    private $conditions;

    public function __construct($conditions) {
        $this->conditions = $conditions;
    }

    public function array():array {

        $data = [
            // 设置表头信息
            ['序号','姓名','车牌号','电话','住址','类型',
                '品牌型号',
                '使用性质',
                '识别代码',
                '发动机号',
                '注册日期',
                '发证日期',
                '档案编号',
                '总质量',
                '核定载人数',
                '创建时间'],
        ];

        $records = (new CertGo())->getLst($this->conditions)->with(['getUser','getGoOther'])->get();

        foreach ($records as $record) {
            $temp = [
                $record->id,
                $record->belong_to??'',
                $record->car_number??'',
                $record->getUser->phone??'',
                $record->address??'',
                $record->car_type??'',
                $record->car_brand??'',
                $record->use_way??'',
                $record->spot_code??'',
                $record->engine_number??'',
                $record->register_day??'',
                $record->giving_day??'',
                $record->getGoOther->file_number??'',
                $record->getGoOther->all_weight??'',
                $record->getGoOther->carry_number??'',
                $record->created_at??'',
            ];
            $data[] = $temp;
        }
        return $data;


    }
    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
